import {
  SHOW_LOADING,
  HIDE_LOADING
} from '../constants/actionType'
import Immutable from 'immutable'

// 我们用引用计数来控制loading的显示隐藏

const initialState = Immutable.fromJS({
  count: 0
})

export default (state = initialState, action) => {
  let newState = state
  switch (action.type) {
    case SHOW_LOADING:
      newState = newState.set('count', state.get('count') + 1)
      return newState
    case HIDE_LOADING:
      newState = newState.set('count', Math.max(state.get('count') - 1))
      return newState
    default :
      return state
  }
}
